 <?php

class Redis_model extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->_redis = $this->redis();
    }

    public function redis() {

        $redis_config = config_item("redis");

        $redis = new redis();
        
        $redis->connect($redis_config["host"]["host"], $redis_config["host"]["port"]);
        if ($redis_config["host"]["password"]) {
            $redis->auth($redis_config["host"]["username"] . ":" . $redis_config["host"]["password"]);
        }
        if ($redis_config["host"]["database"]) {
            $redis->select($redis_config["host"]["database"]);
        }
        return $redis;
    }

    /*
     * 进行队列
     */

    public function rpush($key, $val) {
        $this->_redis->rpush($key, $val);
    }
    
    public function lpush($key, $val) {
        $this->_redis->lpush($key, $val);
    }

    /*
     * 消费队列
     */

    public function lpop($key) {
        return $this->_redis->lpop($key);
    }

    public function lrange($key, $start = 0, $end = -1) {
        return $this->_redis->lrange($key, $start, $end);
    }

}
